package cn.demomaster.quickui.view.banner;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BannerFragmentStateAdapter extends FragmentStateAdapter {

    final OnPlayingListener onloadingListener;
    List<AdsResource> mAdsResourceList;
    //ViewPager2 viewPager2;
    FragmentHander fragmentHander;
    static Class<? extends BannerFragment> claszz;
    public BannerFragmentStateAdapter(@NonNull FragmentActivity fragmentActivity,Class<? extends BannerFragment> claszz, List<AdsResource> adsResourceList, FragmentHander fragmentHander, OnPlayingListener onloadingListener) {
        super(fragmentActivity);
        this.claszz = claszz;
        this.mAdsResourceList = adsResourceList;
        this.onloadingListener = onloadingListener;
        this.fragmentHander = fragmentHander;
        //this.viewPager2 = viewPager2;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        // return mFragments.get(position);
        int position1 = getDataPosition(position);
        BannerFragment fragment = null;
        try {
            Constructor con = claszz.getConstructor(AdsResource.class,OnPlayingListener.class,FragmentHander.class);//.newInstance();
            fragment = (BannerFragment) con.newInstance(mAdsResourceList.get(position1), onloadingListener, fragmentHander);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        }
        //fragment = new BannerFragment2(mAdsResourceList.get(position1), onloadingListener, fragmentHander);
        Bundle bundle = new Bundle();
        bundle.putInt("FragmentIndex",position1);
        fragment.setArguments(bundle);

       /* Class<? extends Fragment> clazz = FragmentFactory.loadFragmentClass(
                viewPager2.getContext().getClassLoader(), BannerFragment.class.getName());
        Fragment fragment = null;
        try {
            fragment = clazz.getConstructor().newInstance();
            Bundle args = new Bundle();
            args.putInt("");
            if (args != null) {
                args.setClassLoader(fragment.getClass().getClassLoader());
                fragment.setArguments(args);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }*/

        registerFragment(fragment);
        return fragment;
    }

    @Override
    public int getItemCount() {
        return (mAdsResourceList == null || mAdsResourceList.size() == 0) ? 0 : Integer.MAX_VALUE;
    }

    Map<Integer, BannerFragmentInterface> adsFragmentMap = new HashMap<>();

    public static int firstFragmentPosition = Integer.MAX_VALUE / 2;

    public int getFirstFragmentPosition() {
        return firstFragmentPosition;
    }

    /**
     * 数据对应的索引
     * @param position
     * @return
     */
    public int getDataPosition(int position) {
        int c =0;
        int itemCount2 = getItemCount2();
        if(itemCount2>0) {
            if (position >= firstFragmentPosition) {
                c = (position - firstFragmentPosition) % itemCount2;
            } else {
                c = itemCount2 - 1 - (firstFragmentPosition - position) % itemCount2;
            }
        }
        return c;
    }

    /**
     * 相对第一个view的位置
     * @param position
     * @return
     */
    public int getRelativePosition(int position) {
        return firstFragmentPosition-position;
    }
    
    /**
     * 真实资源数量
     * @return
     */
    public int getItemCount2() {
        return (mAdsResourceList == null || mAdsResourceList.size() == 0) ? 0 : mAdsResourceList.size();
    }

    public void setData(List<AdsResource> list) {
        mAdsResourceList.clear();
        mAdsResourceList.addAll(list);
    }

    public int getCurrentItem() {
        return fragmentHander.getCurrentItem();
    }

    /**
     * fragment注册激活状态监听
     *
     * @param adsFragment
     */
    public void registerFragment(BannerFragment adsFragment) {
        adsFragmentMap.put(adsFragment.hashCode(), adsFragment);
        //System.out.println("新增页面:" + fragmentCode);
    }
//
//    int mCurrentItemHashCode;
//
//    public void onFragmentActived(int hashCode) {
//        //QdLog.println("onFragmentActived:" + hashCode);
//        mCurrentItemHashCode = hashCode;
//        for (Map.Entry entry : adsFragmentMap.entrySet()) {
//            BannerFragmentInterface anInterface = (BannerFragmentInterface) entry.getValue();
//            anInterface.onActiveChanged(mCurrentItemHashCode);
//        }
//    }

    /**
     * 释放移除的fragment
     *
     * @param fragmentCode
     */
    public void unregisterFragment(int fragmentCode) {
        try {
            adsFragmentMap.remove(fragmentCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface OnPlayingListener {
        void onVideoComplete();//視頻播放完成
    }
}
